{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基于用户的协同过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# coding: utf-8\n",
    "# -*- coding:utf-8 -*-\n",
    "import sys\n",
    "reload(sys)\n",
    "sys.setdefaultencoding(\"utf-8\")\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import cPickle\n",
    "import scipy.io as sio\n",
    "import os\n",
    "\n",
    "#距离\n",
    "import scipy.spatial.distance as ssd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 读入数据做初始化\n",
    "    \n",
    "#用户和item的索引\n",
    "user_index = cPickle.load(open(\"user_index.pkl\", 'rb'))\n",
    "item_index = cPickle.load(open(\"item_index.pkl\", 'rb'))\n",
    "\n",
    "n_users = len(user_index)\n",
    "n_items = len(item_index)\n",
    "    \n",
    "#用户-物品关系矩阵R\n",
    "user_item_scores = sio.mmread(\"user_items_scores\").todense()\n",
    "    \n",
    "#倒排表\n",
    "##每个用户播放的歌曲\n",
    "user_items = cPickle.load(open(\"user_items.pkl\", 'rb'))\n",
    "##事件参加的用户\n",
    "item_users = cPickle.load(open(\"item_users.pkl\", 'rb'))\n",
    "\n",
    "#所有item之间的相似度\n",
    "#similarity_matrix = cPickle.load(open(\"items_similarity.pkl\", 'rb'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算每个用户的平均打分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "mu = np.zeros(n_users)\n",
    "for u in range(n_users):  \n",
    "    n_items_user = 0\n",
    "    r_acc = 0.0\n",
    "    \n",
    "    for i in user_items[u]:  #用户打过分的item\n",
    "        r_acc += user_item_scores[u,i]\n",
    "        n_items_user += 1\n",
    " \n",
    "    mu[u] = r_acc/n_items_user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.00470798,  0.01424275,  0.00961421, ...,  0.08912656,\n",
       "        0.00853275,  0.01698842])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算两个用户之间的相似度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "uid1 = 0\n",
    "uid2 = 1\n",
    "si={}  #有效item（两个用户均有打分的item）的集合\n",
    "for item in user_items[uid1]:  #uid1所有打过分的Item1\n",
    "    if item in user_items[uid2]:  #如果uid2也对该Item打过分\n",
    "        si[item]=1  #item为一个有效item\n",
    "        \n",
    "n=len(si)   #有效item数，有效item为即对uid对Item打过分，uid2也对Item打过分\n",
    "if (n!=0):  #没有共同打过分的item，相似度设为0？\n",
    "    #用户uid1打过分的所有有效的item\n",
    "    s1=np.array([user_item_scores[uid1,item] for item in si])  \n",
    "    s1 -= mu[uid1]\n",
    "        \n",
    "    #用户uid2打过分的所有有效的Item\n",
    "    s2=np.array([user_item_scores[uid2,item] for item in si])\n",
    "    s2 -= mu[uid2]\n",
    "        \n",
    "    similarity = 1 - ssd.cosine(s1, s2)\n",
    "else:\n",
    "    similarity = 0.0; "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def user_similarity(uid1, uid2 ):\n",
    "    si={}  #有效item（两个用户均有打分的item）的集合\n",
    "    for item in user_items[uid1]:  #uid1所有打过分的Item1\n",
    "        if item in user_items[uid2]:  #如果uid2也对该Item打过分\n",
    "            si[item]=1  #item为一个有效item\n",
    "        \n",
    "    n=len(si)   #有效item数，有效item为即对uid对Item打过分，uid2也对Item打过分\n",
    "    if (n==0):  #没有共同打过分的item，相似度设为0？\n",
    "        similarity=0  \n",
    "        return similarity  \n",
    "        \n",
    "    #用户uid1打过分的所有有效的item\n",
    "    s1=np.array([user_item_scores[uid1,item] for item in si])  \n",
    "        \n",
    "    #用户uid2打过分的所有有效的Item\n",
    "    s2=np.array([user_item_scores[uid2,item] for item in si])  \n",
    "        \n",
    "    similarity = 1 - ssd.cosine(s1, s2) \n",
    "    return similarity  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算用户对每个item的预测打分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "cur_user = '5a905f000fc1ff3df7ca807d57edb608863db05d'\n",
    "\n",
    "cur_user_id = user_index[cur_user]\n",
    "cur_user_items = user_items[cur_user_id]\n",
    "\n",
    "sim_accumulate=0.0  \n",
    "rat_acc=0.0 \n",
    "\n",
    "user_items_scores = np.zeros(n_items)\n",
    "\n",
    "for i in range(n_items):  # all items of user not played\n",
    "    if i not in cur_user_items:\n",
    "        for user in item_users[i]:  #对item i打过分的所有用户\n",
    "            #计算当前用户与给item i打过分的用户之间的相似度\n",
    "            sim = user_similarity(uid1 = user,uid2 = cur_user_id)\n",
    "            \n",
    "            if sim != 0: \n",
    "                rat_acc += sim * (user_item_scores[user,i] - mu[user])   #用户user对item i的打分\n",
    "                sim_accumulate += sim  \n",
    "        \n",
    "        if sim_accumulate != 0: #no same user rated,return average rates of the data  \n",
    "            user_items_scores[i] = rat_acc/sim_accumulate    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.01528587,  0.01124084,  0.00907394,  0.00682244,  0.00709192,\n",
       "        0.0060779 ,  0.00547363,  0.00579507,  0.00590985,  0.00587067,\n",
       "        0.00572266,  0.00563748,  0.00553848,  0.00503666,  0.00461434,\n",
       "        0.        ,  0.00421577,  0.00410285,  0.00408508,  0.00379658,\n",
       "        0.00357986,  0.00361372,  0.00336741,  0.00338088,  0.00338154,\n",
       "        0.00338123,  0.        ,  0.00328578,  0.00309455,  0.00299316,\n",
       "        0.00295432,  0.00294884,  0.        ,  0.00300456,  0.003091  ,\n",
       "        0.00294378,  0.        ,  0.00284547,  0.00270577,  0.        ,\n",
       "        0.00270762,  0.00256355,  0.00246766,  0.00252074,  0.00255822,\n",
       "        0.00255406,  0.00255204,  0.00242679,  0.00242947,  0.00244969,\n",
       "        0.00240086,  0.00230993,  0.00221743,  0.00213847,  0.00214668,\n",
       "        0.00210643,  0.00211989,  0.00205667,  0.00202565,  0.00209504,\n",
       "        0.00204785,  0.00204972,  0.00204458,  0.00198225,  0.00190946,\n",
       "        0.00185732,  0.00180234,  0.00181232,  0.        ,  0.00175206,\n",
       "        0.00170327,  0.00166619,  0.00162661,  0.        ,  0.00155278,\n",
       "        0.00151251,  0.00148246,  0.00143296,  0.00142145,  0.00137137,\n",
       "        0.0013797 ,  0.00134161,  0.00131978,  0.00129498,  0.00125186,\n",
       "        0.        ,  0.00122114,  0.00119422,  0.00115428,  0.0011029 ,\n",
       "        0.0011338 ,  0.00110224,  0.00107882,  0.00109422,  0.00109793,\n",
       "        0.00108289,  0.00106186,  0.00104439,  0.00102856,  0.00099767,\n",
       "        0.00097702,  0.00098267,  0.00094543,  0.00091495,  0.        ,\n",
       "        0.0008887 ,  0.00086499,  0.00083909,  0.        ,  0.0008563 ,\n",
       "        0.00083184,  0.00081215,  0.00082215,  0.00080015,  0.00079039,\n",
       "        0.00076436,  0.00075187,  0.00076486,  0.00078253,  0.00078642,\n",
       "        0.00076713,  0.00077789,  0.0007993 ,  0.00077672,  0.00075993,\n",
       "        0.00073578,  0.00072093,  0.00072467,  0.00071519,  0.00069828,\n",
       "        0.00066751,  0.00068752,  0.00067965,  0.00066624,  0.00062628,\n",
       "        0.00063547,  0.00065948,  0.00064029,  0.00063955,  0.00063623,\n",
       "        0.00062572,  0.00062244,  0.00061364,  0.00059366,  0.00057961,\n",
       "        0.00056213,  0.00057168,  0.00056519,  0.00056471,  0.00055985,\n",
       "        0.0005645 ,  0.0005797 ,  0.00058707,  0.00058125,  0.00057343,\n",
       "        0.00058528,  0.00059428,  0.00057808,  0.00057461,  0.00055284,\n",
       "        0.00054185,  0.00052455,  0.00051146,  0.0004978 ,  0.000499  ,\n",
       "        0.00048271,  0.00046718,  0.00044968,  0.00045243,  0.00048052,\n",
       "        0.00046215,  0.00046341,  0.00045569,  0.00044526,  0.        ,\n",
       "        0.00045208,  0.00043542,  0.00042413,  0.00043204,  0.00042641,\n",
       "        0.00041409,  0.00040664,  0.00041289,  0.00042158,  0.00041977,\n",
       "        0.00040491,  0.00040819,  0.00040448,  0.0004158 ,  0.        ,\n",
       "        0.00042307,  0.        ,  0.00040874,  0.00042001,  0.00041518,\n",
       "        0.00041569,  0.0003995 ,  0.00038613,  0.0003779 ,  0.00038495,\n",
       "        0.00038924,  0.        ,  0.00038697,  0.00037826,  0.00036905,\n",
       "        0.00036298,  0.00035851,  0.000352  ,  0.00034385,  0.00034827,\n",
       "        0.00034454,  0.00035494,  0.00035861,  0.00036699,  0.00037384,\n",
       "        0.00037571,  0.00038308,  0.0003783 ,  0.00038225,  0.00037974,\n",
       "        0.00038379,  0.0003892 ,  0.00037899,  0.00036992,  0.00035679,\n",
       "        0.00035546,  0.00035607,  0.00034856,  0.00034923,  0.00034663,\n",
       "        0.00033818,  0.0003471 ,  0.0003405 ,  0.00033034,  0.00033475,\n",
       "        0.00034668,  0.0003614 ,  0.        ,  0.00036225,  0.00036909,\n",
       "        0.00038231,  0.00037361,  0.0003832 ,  0.00038056,  0.0003971 ,\n",
       "        0.00040361,  0.00040988,  0.00040401,  0.00039415,  0.00038998,\n",
       "        0.00038758,  0.0003821 ,  0.00039397,  0.00038767,  0.00038465,\n",
       "        0.        ,  0.00037677,  0.00036747,  0.00036957,  0.00036492,\n",
       "        0.00036861,  0.0003635 ,  0.00036691,  0.00036163,  0.00036357,\n",
       "        0.00036479,  0.00036954,  0.00036876,  0.00036472,  0.00037098,\n",
       "        0.00038143,  0.00037151,  0.00037551,  0.00037631,  0.00037944,\n",
       "        0.00038066,  0.00037391,  0.00037107,  0.00036585,  0.00036229,\n",
       "        0.00035925,  0.00036687,  0.00038179,  0.00037147,  0.00038302,\n",
       "        0.00038535,  0.00038235,  0.00039771,  0.00039252,  0.00038581,\n",
       "        0.00038183,  0.00037871,  0.00038193,  0.00037192,  0.00036986,\n",
       "        0.00037005,  0.00037543,  0.00036449,  0.00036801,  0.00038808,\n",
       "        0.00038218,  0.0003841 ,  0.00037402,  0.00037379,  0.00037549,\n",
       "        0.0003747 ,  0.00038366,  0.00037625,  0.0003674 ,  0.00036829,\n",
       "        0.00036041,  0.00036261,  0.00037202,  0.0003748 ,  0.0003665 ,\n",
       "        0.00036928,  0.00036264,  0.00036129,  0.00036076,  0.00035029,\n",
       "        0.00035458,  0.00035575,  0.        ,  0.00036293,  0.00037016,\n",
       "        0.00037709,  0.00037528,  0.00037863,  0.00037656,  0.00036903,\n",
       "        0.00036894,  0.00036633,  0.00036267,  0.00035497,  0.0003684 ,\n",
       "        0.        ,  0.00037013,  0.00036568,  0.00036754,  0.00035512,\n",
       "        0.00034959,  0.00034759,  0.00034932,  0.00034927,  0.00034685,\n",
       "        0.00034102,  0.000337  ,  0.00032937,  0.00033274,  0.00033783,\n",
       "        0.00034038,  0.00033732,  0.00033793,  0.00034018,  0.00033606,\n",
       "        0.        ,  0.00034053,  0.00034271,  0.00034927,  0.0003442 ,\n",
       "        0.00034403,  0.00034264,  0.00034047,  0.00033778,  0.00033507,\n",
       "        0.00033679,  0.00033818,  0.00033326,  0.00033354,  0.00032954,\n",
       "        0.00032374,  0.00032945,  0.00033655,  0.00033614,  0.00033172,\n",
       "        0.        ,  0.00033314,  0.00033366,  0.00033582,  0.00033488,\n",
       "        0.00034742,  0.0003509 ,  0.00034504,  0.00035619,  0.00035166,\n",
       "        0.00034986,  0.00034678,  0.00034544,  0.00034786,  0.00034587,\n",
       "        0.00034117,  0.00033854,  0.00034336,  0.00034428,  0.00034177,\n",
       "        0.00034201,  0.00033688,  0.00033558,  0.00033906,  0.00034646,\n",
       "        0.        ,  0.00034437,  0.00033979,  0.00033595,  0.00034125,\n",
       "        0.00033876,  0.00034005,  0.        ,  0.00033694,  0.00033306,\n",
       "        0.00033442,  0.0003324 ,  0.00032924,  0.0003204 ,  0.00031857,\n",
       "        0.00031742,  0.00032136,  0.00032019,  0.00031725,  0.00030725,\n",
       "        0.00030643,  0.00030849,  0.00030804,  0.        ,  0.00031289,\n",
       "        0.0003099 ,  0.00030768,  0.00031014,  0.00031365,  0.00031382,\n",
       "        0.00030888,  0.00030424,  0.00030025,  0.00031103,  0.00031799,\n",
       "        0.0003157 ,  0.00031319,  0.00031703,  0.00031473,  0.00031347,\n",
       "        0.00031696,  0.00032038,  0.00031955,  0.00031759,  0.00031453,\n",
       "        0.00031751,  0.00031388,  0.00031109,  0.00030576,  0.00029852,\n",
       "        0.00029538,  0.        ,  0.00028943,  0.00029039,  0.00028804,\n",
       "        0.00029505,  0.00029448,  0.0002985 ,  0.00029808,  0.00029641,\n",
       "        0.00030407,  0.00030309,  0.00030182,  0.00030851,  0.00031291,\n",
       "        0.00031   ,  0.00030984,  0.00031127,  0.00030907,  0.00030903,\n",
       "        0.00029923,  0.00029563,  0.0002924 ,  0.        ,  0.00029121,\n",
       "        0.00028553,  0.        ,  0.00029596,  0.00029559,  0.00029419,\n",
       "        0.00029432,  0.00029093,  0.00028837,  0.00028817,  0.00028615,\n",
       "        0.00028635,  0.00029025,  0.00029079,  0.00028832,  0.00028979,\n",
       "        0.00028932,  0.00028308,  0.00028105,  0.00028013,  0.00027947,\n",
       "        0.00027985,  0.0002791 ,  0.00028384,  0.00028902,  0.00029965,\n",
       "        0.00029425,  0.00029504,  0.00029624,  0.00030036,  0.00029806,\n",
       "        0.0002976 ,  0.00029515,  0.00029318,  0.00029051,  0.0002849 ,\n",
       "        0.00028211,  0.00027939,  0.00027965,  0.00027522,  0.00027549,\n",
       "        0.00027301,  0.        ,  0.00026505,  0.00026649,  0.        ,\n",
       "        0.00025956,  0.        ,  0.00025949,  0.        ,  0.00025732,\n",
       "        0.00025725,  0.        ,  0.00026036,  0.00025483,  0.00025087,\n",
       "        0.00024948,  0.00023268,  0.00023297,  0.00022824,  0.0002306 ,\n",
       "        0.00022851,  0.0002309 ,  0.00023156,  0.00023023,  0.00022904,\n",
       "        0.00022537,  0.00022797,  0.00022765,  0.00022313,  0.00023297,\n",
       "        0.00023328,  0.00022921,  0.        ,  0.00022985,  0.00023698,\n",
       "        0.00024021,  0.00023913,  0.00024281,  0.00024267,  0.0002444 ,\n",
       "        0.00024302,  0.00024047,  0.00024279,  0.00023903,  0.00023686,\n",
       "        0.00023529,  0.00023615,  0.00023814,  0.00024174,  0.0002388 ,\n",
       "        0.00023793,  0.        ,  0.00023671,  0.00023469,  0.00023213,\n",
       "        0.00022988,  0.00023329,  0.00022968,  0.00023296,  0.00023338,\n",
       "        0.00023038,  0.        ,  0.0002274 ,  0.000228  ,  0.00022923,\n",
       "        0.        ,  0.00022731,  0.0002268 ,  0.00022627,  0.00022388,\n",
       "        0.00022515,  0.00022213,  0.00022254,  0.        ,  0.00021869,\n",
       "        0.00021582,  0.00021566,  0.00021745,  0.00022194,  0.00021995,\n",
       "        0.00021696,  0.00021278,  0.00021071,  0.00021182,  0.00021105,\n",
       "        0.00020654,  0.00021085,  0.000208  ,  0.00020697,  0.00020795,\n",
       "        0.00020617,  0.00020569,  0.        ,  0.00020756,  0.00020688,\n",
       "        0.00020651,  0.00020807,  0.0002048 ,  0.00020549,  0.0002041 ,\n",
       "        0.00020846,  0.00021127,  0.00021641,  0.00021884,  0.00022011,\n",
       "        0.00022132,  0.0002252 ,  0.00022228,  0.00022064,  0.00021956,\n",
       "        0.00022032,  0.00022146,  0.00022276,  0.00021997,  0.00022666,\n",
       "        0.0002353 ,  0.00023663,  0.00023183,  0.00023136,  0.00023076,\n",
       "        0.00022914,  0.00022592,  0.00022787,  0.00022854,  0.00022967,\n",
       "        0.00023274,  0.00023016,  0.00023032,  0.00022665,  0.0002306 ,\n",
       "        0.00023221,  0.00023152,  0.00023257,  0.00022975,  0.00022734,\n",
       "        0.00022458,  0.00022168,  0.0002253 ,  0.00022485,  0.00022582,\n",
       "        0.00022683,  0.00022456,  0.00022697,  0.00022864,  0.00023098,\n",
       "        0.00022985,  0.00022363,  0.00022027,  0.0002167 ,  0.000215  ,\n",
       "        0.0002145 ,  0.00021149,  0.00021564,  0.00021405,  0.0002137 ,\n",
       "        0.00021782,  0.00021582,  0.00021118,  0.00020965,  0.0002111 ,\n",
       "        0.0002072 ,  0.00020336,  0.00020748,  0.        ,  0.00020714,\n",
       "        0.00020453,  0.00020132,  0.0001982 ,  0.0001981 ,  0.00019585,\n",
       "        0.00019364,  0.0001913 ,  0.00018372,  0.        ,  0.0001823 ,\n",
       "        0.00018569,  0.000187  ,  0.00018892,  0.00018919,  0.00018827,\n",
       "        0.00018782,  0.00018609,  0.00018748,  0.        ,  0.00018892,\n",
       "        0.00018745,  0.00018284,  0.00017905,  0.00017418,  0.00017686,\n",
       "        0.00017572,  0.00017536,  0.00017226,  0.00017276,  0.00017047,\n",
       "        0.00016866,  0.00016776,  0.00016771,  0.00016475,  0.00016106,\n",
       "        0.0001591 ,  0.00016049,  0.00016134,  0.00016369,  0.00016283,\n",
       "        0.0001603 ,  0.00015896,  0.00015978,  0.00016036,  0.00016639,\n",
       "        0.00016545,  0.00017781,  0.        ,  0.00017795,  0.00017987,\n",
       "        0.00017873,  0.00018014,  0.00017873,  0.00017712,  0.00017442,\n",
       "        0.00017503,  0.00017306,  0.00017074,  0.00016734,  0.00016888,\n",
       "        0.00016855,  0.00016784,  0.00016538,  0.0001637 ,  0.00016312,\n",
       "        0.00016045,  0.00016003,  0.        ,  0.        ,  0.00015728,\n",
       "        0.00015717,  0.00015514,  0.00015195,  0.00015583,  0.00015566,\n",
       "        0.00015315,  0.00015278,  0.00014894,  0.00014473,  0.00014148,\n",
       "        0.00013801,  0.00014094,  0.00014494,  0.        ,  0.00014589,\n",
       "        0.00014467,  0.00014396,  0.00014266,  0.00014137,  0.00014111,\n",
       "        0.00013784,  0.0001377 ,  0.00013735,  0.00014001,  0.00014153,\n",
       "        0.00013743,  0.00013561,  0.00013434,  0.00013255,  0.00013048,\n",
       "        0.0001295 ,  0.00012786,  0.0001298 ,  0.00012887,  0.00013   ,\n",
       "        0.00013293,  0.00013231,  0.00013306,  0.00013671,  0.00013797,\n",
       "        0.00013619,  0.00013228,  0.00013149,  0.00012895,  0.0001296 ,\n",
       "        0.00013016,  0.00013072,  0.00012835,  0.0001285 ,  0.00013064,\n",
       "        0.000129  ,  0.0001305 ,  0.00012898,  0.00012673,  0.00012735,\n",
       "        0.00012216,  0.0001243 ,  0.00012615,  0.00012726,  0.00012475,\n",
       "        0.00012292,  0.00012528,  0.00012578,  0.00012562,  0.00012541,\n",
       "        0.00013002,  0.00012907,  0.00012767,  0.        ,  0.00012572,\n",
       "        0.00012548,  0.00012655,  0.00012473,  0.0001244 ,  0.00012885,\n",
       "        0.00012995,  0.        ,  0.00013397,  0.00013351,  0.00013333,\n",
       "        0.00013709,  0.00013669,  0.00013465,  0.00013465,  0.        ,\n",
       "        0.00013513,  0.00013634,  0.00013873,  0.00013593,  0.00013972,\n",
       "        0.00013897,  0.00014058,  0.00014128,  0.00013518,  0.00013715,\n",
       "        0.0001388 ,  0.00013878,  0.00013825,  0.0001358 ,  0.00013289,\n",
       "        0.        ,  0.00013435,  0.00013685,  0.00013492,  0.00013378,\n",
       "        0.00013394,  0.        ,  0.00013442,  0.00013285,  0.000133  ,\n",
       "        0.00013198,  0.00012973,  0.00013074,  0.0001371 ,  0.00013884,\n",
       "        0.00013669,  0.00013727,  0.00013475,  0.00013423,  0.00013267,\n",
       "        0.00013085,  0.00013228,  0.00013129,  0.00012977,  0.00013113,\n",
       "        0.00012876,  0.00012706,  0.        ,  0.00012536,  0.00012219,\n",
       "        0.00012183,  0.00011715,  0.00011864,  0.00011657,  0.00011556,\n",
       "        0.00011886,  0.00011527,  0.00011462,  0.00011755,  0.00012015,\n",
       "        0.00012019,  0.00011997,  0.00012147,  0.00012102,  0.        ,\n",
       "        0.00012101,  0.00012207,  0.00012127,  0.0001227 ,  0.00012064,\n",
       "        0.00012037,  0.00011931,  0.00011807,  0.00011526,  0.00011639,\n",
       "        0.0001166 ,  0.00011962,  0.00011755,  0.00012645,  0.00012801,\n",
       "        0.0001341 ,  0.0001313 ,  0.00012938,  0.        ,  0.00012857,\n",
       "        0.00012757,  0.00012714,  0.        ,  0.00012553,  0.00012644,\n",
       "        0.00012687,  0.        ,  0.        ,  0.00012636,  0.00012911,\n",
       "        0.00012955,  0.00012988,  0.00013065,  0.00013162,  0.000132  ,\n",
       "        0.00012919,  0.00012966,  0.0001296 ,  0.00012956,  0.00013802,\n",
       "        0.00013787,  0.000137  ,  0.0001374 ,  0.00013845,  0.00013966,\n",
       "        0.00013587,  0.00013464,  0.00013201,  0.00013127,  0.00013248,\n",
       "        0.00012911,  0.00012792,  0.00012587,  0.00012518,  0.00012379,\n",
       "        0.00012581,  0.00012852,  0.00013093,  0.00012874,  0.        ,\n",
       "        0.00012989,  0.00012718,  0.00012694,  0.00013128,  0.00013045,\n",
       "        0.00012933,  0.00012879,  0.00012608,  0.00012423,  0.00012388,\n",
       "        0.00012229,  0.00012456,  0.00012227,  0.00012094,  0.00012224,\n",
       "        0.00012108,  0.00012071,  0.00012087,  0.00011869,  0.00011918,\n",
       "        0.0001109 ,  0.00011006,  0.00010898,  0.00010993,  0.00011443,\n",
       "        0.00011265,  0.00010777,  0.00010761,  0.0001075 ,  0.00010584,\n",
       "        0.0001056 ,  0.0001062 ,  0.00010655,  0.0001074 ,  0.00010674,\n",
       "        0.00011125,  0.00010965,  0.00010661,  0.00010496,  0.00010642,\n",
       "        0.00010602,  0.00010777,  0.00011066,  0.00011026,  0.00011225])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_items_scores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 根据用户对item的预测打分产生top推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Sort the indices of user_item_scores based upon their value\n",
    "#Also maintain the corresponding score\n",
    "sort_index = sorted(((e,i) for i,e in enumerate(list(user_items_scores))), reverse=True)\n",
    "    \n",
    "#Create a dataframe from the following\n",
    "columns = ['user_id', 'item', 'score', 'rank']\n",
    "df = pd.DataFrame(columns=columns)\n",
    "         \n",
    "#Fill the dataframe with top 20 item based recommendations\n",
    "rank = 1 \n",
    "for i in range(0,len(sort_index)):\n",
    "    cur_item_index = sort_index[i][1] \n",
    "    cur_item = list (item_index.keys()) [list (item_index.values()).index (cur_item_index)]\n",
    "            \n",
    "    if ~np.isnan(sort_index[i][0]) and cur_item_index not in cur_user_items and rank <= 20:\n",
    "        df.loc[len(df)]=[cur_user,cur_item,sort_index[i][0],rank]\n",
    "        rank = rank+1       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOBONKR12A58A7A7E0</td>\n",
       "      <td>0.015286</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAUWYT12A81C206F1</td>\n",
       "      <td>0.011241</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOSXLTC12AF72A7F54</td>\n",
       "      <td>0.009074</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOEGIYH12A6D4FC0E3</td>\n",
       "      <td>0.007092</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOFRQTD12A81C233C0</td>\n",
       "      <td>0.006822</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAXGDH12A8C13F8A1</td>\n",
       "      <td>0.006078</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUFTBI12AB0183F65</td>\n",
       "      <td>0.005910</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOVDSJC12A58A7A271</td>\n",
       "      <td>0.005871</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOPUCYA12A8C13A694</td>\n",
       "      <td>0.005795</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOOFYTN12A6D4F9B35</td>\n",
       "      <td>0.005723</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOHTKMO12AB01843B0</td>\n",
       "      <td>0.005637</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOBOUPA12A6D4F81F1</td>\n",
       "      <td>0.005538</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SONYKOW12AB01849C9</td>\n",
       "      <td>0.005474</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SODJWHY12A8C142CCE</td>\n",
       "      <td>0.005037</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOLFXKT12AB017E3E0</td>\n",
       "      <td>0.004614</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOFLJQZ12A6D4FADA6</td>\n",
       "      <td>0.004216</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOTWNDJ12A8C143984</td>\n",
       "      <td>0.004103</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUNZHU12A8AE47481</td>\n",
       "      <td>0.004085</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUVTSM12AC468F6A7</td>\n",
       "      <td>0.003797</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUDLVN12AAFF43658</td>\n",
       "      <td>0.003614</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     user_id                item     score  \\\n",
       "0   5a905f000fc1ff3df7ca807d57edb608863db05d  SOBONKR12A58A7A7E0  0.015286   \n",
       "1   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAUWYT12A81C206F1  0.011241   \n",
       "2   5a905f000fc1ff3df7ca807d57edb608863db05d  SOSXLTC12AF72A7F54  0.009074   \n",
       "3   5a905f000fc1ff3df7ca807d57edb608863db05d  SOEGIYH12A6D4FC0E3  0.007092   \n",
       "4   5a905f000fc1ff3df7ca807d57edb608863db05d  SOFRQTD12A81C233C0  0.006822   \n",
       "5   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAXGDH12A8C13F8A1  0.006078   \n",
       "6   5a905f000fc1ff3df7ca807d57edb608863db05d  SOUFTBI12AB0183F65  0.005910   \n",
       "7   5a905f000fc1ff3df7ca807d57edb608863db05d  SOVDSJC12A58A7A271  0.005871   \n",
       "8   5a905f000fc1ff3df7ca807d57edb608863db05d  SOPUCYA12A8C13A694  0.005795   \n",
       "9   5a905f000fc1ff3df7ca807d57edb608863db05d  SOOFYTN12A6D4F9B35  0.005723   \n",
       "10  5a905f000fc1ff3df7ca807d57edb608863db05d  SOHTKMO12AB01843B0  0.005637   \n",
       "11  5a905f000fc1ff3df7ca807d57edb608863db05d  SOBOUPA12A6D4F81F1  0.005538   \n",
       "12  5a905f000fc1ff3df7ca807d57edb608863db05d  SONYKOW12AB01849C9  0.005474   \n",
       "13  5a905f000fc1ff3df7ca807d57edb608863db05d  SODJWHY12A8C142CCE  0.005037   \n",
       "14  5a905f000fc1ff3df7ca807d57edb608863db05d  SOLFXKT12AB017E3E0  0.004614   \n",
       "15  5a905f000fc1ff3df7ca807d57edb608863db05d  SOFLJQZ12A6D4FADA6  0.004216   \n",
       "16  5a905f000fc1ff3df7ca807d57edb608863db05d  SOTWNDJ12A8C143984  0.004103   \n",
       "17  5a905f000fc1ff3df7ca807d57edb608863db05d  SOUNZHU12A8AE47481  0.004085   \n",
       "18  5a905f000fc1ff3df7ca807d57edb608863db05d  SOUVTSM12AC468F6A7  0.003797   \n",
       "19  5a905f000fc1ff3df7ca807d57edb608863db05d  SOUDLVN12AAFF43658  0.003614   \n",
       "\n",
       "   rank  \n",
       "0     1  \n",
       "1     2  \n",
       "2     3  \n",
       "3     4  \n",
       "4     5  \n",
       "5     6  \n",
       "6     7  \n",
       "7     8  \n",
       "8     9  \n",
       "9    10  \n",
       "10   11  \n",
       "11   12  \n",
       "12   13  \n",
       "13   14  \n",
       "14   15  \n",
       "15   16  \n",
       "16   17  \n",
       "17   18  \n",
       "18   19  \n",
       "19   20  "
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
